/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.windesheim.archdemo.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

/**
 *
 * @author re0053273
 */
@Entity(name = "my_order") // needs another name since Order is reserved word in SQL
public class Order implements Serializable {

    private int id;
    private Customer customer;
    private Date creationDate;
    private String status;
    private List<OrderLine> orderLines;

    @NotNull
    @ManyToOne
    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreationDate() {
        return creationDate;
    }

    /**
     * Accepts creationDate as string with format "dd-MM-yyyy"
     * @param dateString 
     */
    public void setCreationDateString(String dateString) {
        this.creationDate = DateTime.parse(dateString, DateTimeFormat.forPattern("dd-MM-yyyy")).toDate();
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
   
    // FIXME: getorderlines should be lazy (but gives transaction problem in view)
    @OneToMany(mappedBy = "order", fetch= FetchType.EAGER)
    public List<OrderLine> getOrderLines() {
        return orderLines;
    }

    public void setOrderLines(List<OrderLine> orderLines) {
        this.orderLines = orderLines;
    }
    
}
